CC = gcc # C compiler
CXX = g++
LIB_NAME := c_functions.so # target lib
LIB_C_SRCS := $(wildcard *.c)
LIB_CXX_SRCS := $(wildcard *.cpp)
LIB_C_OBJS := ${LIB_C_SRCS:.c=.o}
LIB_CXX_OBJS := ${LIB_CXX_SRCS:.cpp=.o}
LIB_OBJS := $(LIB_C_OBJS) $(LIB_CXX_OBJS)
LIB_INCLUDE_DIRS :=
LIB_LIBRARY_DIRS :=
LIB_LIBRARIES :=

CFLAGS = -fPIC -Wall -Wextra -O2 -g -pedantic -std=gnu99 # C flags
CPPFLAGS = -fPIC -Wall -ggdb
LDFLAGS = -shared -lstdc++ # linking flags, I need the -lstdc++ to link with cpp sources
RM = rm -f  # rm command
CPPFLAGS += $(foreach includedir,$(LIB_INCLUDE_DIRS),-I$(includedir))
LDFLAGS += $(foreach librarydir,$(LIB_LIBRARY_DIRS),-L$(librarydir))
LDFLAGS += $(foreach library,$(LIB_LIBRARIES),-l$(library))

.PHONY: all clean distclean
 
all: $(LIB_NAME)

$(LIB_NAME): $(LIB_OBJS)
	$(CC) ${LDFLAGS} -o $@ $^
    #$(LINK.cc) $(LIB_OBJS) -o $(LIB_NAME)

$(LIB_C_SRCS:.c=.d):%.d:%.c
	$(CC) $(CFLAGS) -MM $< >$@

$(LIB_CXX_SRCS:.cpp=.d):%.d:%.ccp
	$(CXX) $(CPPFLAGS) -MM $< >$@

clean:
	@- $(RM) $(LIB_NAME)
	@- $(RM) $(LIB_OBJS)

distclean: clean